package org.jeecg.modules.workvehicleborrow.controller;

import java.util.Arrays;
import java.util.List;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.modules.vehicle.service.IWorkVehicleService;
import org.jeecg.modules.workvehicleborrow.entity.WorkVehicleBorrow;
import org.jeecg.modules.workvehicleborrow.service.IWorkVehicleBorrowService;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;

import org.jeecg.modules.workvehicleborrow.vo.workvehicleborrowVo;
import org.jeecg.common.system.base.controller.JeecgController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.jeecg.common.aspect.annotation.AutoLog;

 /**
 * @Description: work_vehicle_borrow
 * @Author: jeecg-boot
 * @Date:   2021-08-25
 * @Version: V1.0
 */
@Api(tags="work_vehicle_borrow")
@RestController
@RequestMapping("/workvehicleborrow/workVehicleBorrow")
@Slf4j
public class WorkVehicleBorrowController extends JeecgController<WorkVehicleBorrow, IWorkVehicleBorrowService> {
	@Autowired
	private IWorkVehicleBorrowService workVehicleBorrowService;

	
	/**
	 * 分页列表查询
	 *
	 * @param workvehicleborrowVo
	 * @param pageNo
	 * @param pageSize
	 * @param req
	 * @return
	 */
	@AutoLog(value = "work_vehicle_borrow-分页列表查询")
	@ApiOperation(value="work_vehicle_borrow-分页列表查询", notes="work_vehicle_borrow-分页列表查询")
	@GetMapping(value = "/list")
	public Result<?> queryPageList(workvehicleborrowVo workvehicleborrowVo,
								   @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
								   @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
								   HttpServletRequest req, String borrowStartTime, String vehicleNumber) {
		QueryWrapper<workvehicleborrowVo> queryWrapper = QueryGenerator.initQueryWrapper(workvehicleborrowVo, req.getParameterMap());
		Page<workvehicleborrowVo> page = new Page<workvehicleborrowVo>(pageNo, pageSize);
		List<workvehicleborrowVo> pageList = workVehicleBorrowService.findworkvehicleborrow(page,borrowStartTime,vehicleNumber);
		System.out.println(pageList);
		return Result.OK(pageList);
	}


	 /**
	  *  编辑
	  *
	  * @param workvehicleborrowVo
	  * @return
	  */
	 @AutoLog(value = "work_vehicle_borrow-车俩分配")
	 @ApiOperation(value="work_vehicle_borrow-车俩分配", notes="work_vehicle_borrow-车俩分配")
	 @PostMapping(value = "/addchelia")
	 public Result addchelia(@RequestBody workvehicleborrowVo workvehicleborrowVo) {


		 System.out.println(workvehicleborrowVo.getVehicleState());
		 String startTime=workVehicleBorrowService.findstartTime(workvehicleborrowVo.getBorrowId());
		 System.out.println(startTime);
;
         if (startTime==null){
		 	if (workvehicleborrowVo.getVehicleState()==0){
				workVehicleBorrowService.insert(workvehicleborrowVo);
				return Result.OK("分配车俩成功！");
			}else {
				return Result.error("该车俩在维修或已借用，请换车！");
			}
		 }else {
			 return Result.error("该记录已借用了车俩！");
		 }
	 }
	/**
	 *   添加
	 *
	 * @param workVehicleBorrow
	 * @return
	 */
	@AutoLog(value = "work_vehicle_borrow-添加")
	@ApiOperation(value="work_vehicle_borrow-添加", notes="work_vehicle_borrow-添加")
	@PostMapping(value = "/add")
	public Result<?> add(@RequestBody WorkVehicleBorrow workVehicleBorrow) {
		System.out.println(workVehicleBorrow);
		workVehicleBorrowService.save(workVehicleBorrow);
		return Result.OK("添加成功！");
	}
	
	/**
	 *  编辑
	 *
	 * @param workVehicleBorrow
	 * @return
	 */
	@AutoLog(value = "work_vehicle_borrow-编辑")
	@ApiOperation(value="work_vehicle_borrow-编辑", notes="work_vehicle_borrow-编辑")
	@PutMapping(value = "/edit")
	public Result<?> edit(@RequestBody WorkVehicleBorrow workVehicleBorrow) {
		String endTime=workVehicleBorrowService.findendTime(workVehicleBorrow.getBorrowId());
		if (endTime==null){
			workVehicleBorrowService.updateById(workVehicleBorrow);
			return Result.OK("编辑成功!");
		}else {
			workVehicleBorrowService.updateById(workVehicleBorrow);
			return Result.error("已完成了借用，不能编辑!!");
		}

	}
	
	/**
	 *   通过id删除
	 *
	 * @param id
	 * @return
	 */
	@AutoLog(value = "work_vehicle_borrow-通过id删除")
	@ApiOperation(value="work_vehicle_borrow-通过id删除", notes="work_vehicle_borrow-通过id删除")
	@DeleteMapping(value = "/delete")
	public Result<?> delete(@RequestParam(name="id",required=true) String id) {
		String endTime=workVehicleBorrowService.findendTime(id);
		if (endTime==null){
			int i=workVehicleBorrowService.del(id);
			return Result.OK("删除成功!");
		}else {
			return Result.error("已完成了借用，不能删除!");
		}
	}
	
	/**
	 *  批量删除
	 *
	 * @param ids
	 * @return
	 */
	@AutoLog(value = "work_vehicle_borrow-批量删除")
	@ApiOperation(value="work_vehicle_borrow-批量删除", notes="work_vehicle_borrow-批量删除")
	@DeleteMapping(value = "/deleteBatch")
	public Result<?> deleteBatch(@RequestParam(name="ids",required=true) String ids) {
		this.workVehicleBorrowService.removeByIds(Arrays.asList(ids.split(",")));
		return Result.OK("批量删除成功!");
	}
	
	/**
	 * 通过id查询
	 *
	 * @param id
	 * @return
	 */
	@AutoLog(value = "work_vehicle_borrow-通过id查询")
	@ApiOperation(value="work_vehicle_borrow-通过id查询", notes="work_vehicle_borrow-通过id查询")
	@GetMapping(value = "/queryById")
	public Result<?> queryById(@RequestParam(name="id",required=true) String id) {
		WorkVehicleBorrow workVehicleBorrow = workVehicleBorrowService.getById(id);
		if(workVehicleBorrow==null) {
			return Result.error("未找到对应数据");
		}
		return Result.OK(workVehicleBorrow);
	}

    /**
    * 导出excel
    *
    * @param request
    * @param workVehicleBorrow
    */
    @RequestMapping(value = "/exportXls")
    public ModelAndView exportXls(HttpServletRequest request, WorkVehicleBorrow workVehicleBorrow) {
        return super.exportXls(request, workVehicleBorrow, WorkVehicleBorrow.class, "work_vehicle_borrow");
    }

    /**
      * 通过excel导入数据
    *
    * @param request
    * @param response
    * @return
    */
    @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
    public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
        return super.importExcel(request, response, WorkVehicleBorrow.class);
    }

}
